package com.fengye.api.controller;

import com.fengye.common.controller.BaseController;
import com.fengye.common.annotation.CurrentUserId;
import com.fengye.common.domain.ResponseResult;
import com.fengye.common.domain.dto.UserLoginDto;
import com.fengye.common.domain.entity.User;
import com.fengye.common.utils.I18nUtil;
import com.fengye.security.service.UserService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import jakarta.validation.Valid;
import java.util.*;

/**
 * @author sunlei
 * @description 用户模块
 */
@Api(tags = "用户模块")
@RestController
@RequestMapping("/user")
@CrossOrigin
@Slf4j
public class UserController extends BaseController {
    @Autowired
    private UserService userService;

    /**
     * 注册用户
     * @param user 用户实体类
     * @return 结果
     */
    @RequestMapping(value = "/reg", method={RequestMethod.POST})
    public ResponseResult<Void> reg(@RequestBody User user) {
        userService.reg(user);

        return ResponseResult.success(I18nUtil.message("user.reg.success"));
    }

    /**
     * 用户登录
     * @param userLoginDto 用户dto
     * @return 结果
     */
    @RequestMapping(value = "/login", method = {RequestMethod.POST})
    public ResponseResult<Map<String, Object>> login(@Validated @RequestBody UserLoginDto userLoginDto) {
        Map<String, Object> loginResult = userService.login(userLoginDto.getUsername(), userLoginDto.getPassword());
        log.info(I18nUtil.message("user.login.success"));
        return ResponseResult.success(I18nUtil.message("user.login.success"), loginResult);
    }

    /**
     * 退出登录
     * @return 结果
     */
    @PostMapping("/logout")
    public ResponseResult<Void> logout(@CurrentUserId @ApiIgnore Long userId) {
        userService.logout(userId);
        return ResponseResult.success(I18nUtil.message("user.logout.success"));
    }

    /**
     * 修改密码
     * @param userId 用户id
     * @param oldPassword 旧密码
     * @param newPassword 新密码
     * @return 结果
     */
    @RequestMapping(value = "/change/password", method = {RequestMethod.POST})
    @PreAuthorize("hasAuthority('system:dept:list')")
    public ResponseResult<Void> changePassword(@CurrentUserId @ApiIgnore String userId, String oldPassword, String newPassword) {
        userService.changePassword(Long.valueOf(userId), oldPassword, newPassword);
        return ResponseResult.success(I18nUtil.message("user.modify.pwd.success"));
    }

    @RequestMapping(path = "/info", method = {RequestMethod.GET})
    public ResponseResult<User> getUserinfo(@CurrentUserId @ApiIgnore Long userId) {
        User result = userService.getUserinfo(userId);
        return ResponseResult.success(result);
    }

    @RequestMapping(path = "/change/info", method = {RequestMethod.POST})
    public ResponseResult<Void> changeUserinfo(@RequestBody @Valid User user) {
        userService.changeUserinfo(user);
        return ResponseResult.success(I18nUtil.message("user.modify.info.success"));
    }
}
